<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:01
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\common\business;

use app\common\lib\Status;
use app\common\validate\Menu as ValObj;
use app\common\model\mysql\Menu as ModelObj;
use think\Exception;
use think\facade\Cache;

class Menu extends Base
{
    /**
     * @var ValObj
     */
    protected ValObj $valObj;
    /**
     * @var ModelObj
     */
    protected ModelObj $modelObj;

    /**
     *
     */
    public function __construct()
    {
        $this->valObj = new ValObj();
        $this->modelObj = new ModelObj();
    }

    /**
     * @param array $data
     * @return bool
     * @throws Exception
     */
    public function add(array $data = []): bool
    {
        //验证字段
        $this->isValidateAdd($data);
        //添加入库
//        dump($data);exit();
        try {
            $res = $this->modelObj->insertAdd($data);
        }catch (\Exception $e){
            throw new Exception("添加失败");
        }
        if ($res) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSystemInit(int $userId = 0)
    {
        $homeInfo = [
            'title' => '首页',
            'href' => (string)url('welcome/index'),
        ];
        $logoInfo = [
            'title' => sysconf('sitename'),
            'image' => '/' . sysconf('icon'),
        ];
        $menuInfo = $this->getMenuByListInfo($userId);

        return [
            'homeInfo' => $homeInfo,
            'logoInfo' => $logoInfo,
            'menuInfo' => $menuInfo,
        ];
    }

    /**
     * 查询菜单 返回无限极菜单
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMenuByListInfo(int $rule_id = 0)
    {
        $menu = $this->modelObj->getMenuByListInfo("*",$rule_id);
        if (empty($menu)) {
            return [];
        }
        return \app\common\lib\Menu::buildMenuChild(0, $menu);
    }

    /**
     *
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMenuByListInfoTree(): array
    {
        $menu = $this->modelObj->getMenuByListInfo('id,id as value,pid,title as name');
        if (empty($menu)) {
            return [];
        }
        return \app\common\lib\Menu::buildMenuChild(0, $menu, 'children');
    }

    /**
     * 获取首页
     * @param int $pid
     * @return mixed
     */
    public function getMenuByHomeId(int $pid = 0)
    {
        return $this->modelObj->getMenuByHomeId($pid);
    }

    public function getSystemInitMenu()
    {
        $menu = $this->modelObj->getMenuByListInfo();
        return \app\common\lib\Menu::buildMenuChild(0, $menu, 'treeList');
    }
    public function getSystemInitMenuList($field)
    {
        $menu = $this->modelObj->getMenuByListInfo($field);
        //$res_menu = \app\common\lib\Menu::buildMenuChild(0, $menu, 'submenu');
        return $menu;
    }

}
